<article id="wikiArticle">
<div></div>
<h2 id="Summary" name="Summary">概述</h2>
<p><strong>has() </strong>方法返回一个布尔值来指示对应的值value是否存在Set对象中。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><code><em>mySet</em>.has(value);</code></code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt>value</dt>
<dd>必需。用以测试该值是否存在于 Set 对象中。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<dl>
<dt>Boolean</dt>
<dd>如果指定的值（value）存在于Set对象当中，返回<code>true</code>；否则返回 <code>false</code>。</dd>
</dl>
<h2 id="Examples" name="Examples">示例</h2>
<h3 id="Example:_Testing_size_of_all_array_elements" name="Example:_Testing_size_of_all_array_elements">使用 <code>has</code> 方法</h3>
<pre><code  class="language-javascript"><code class="language-js"><span class="keyword token">var</span> mySet <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
mySet<span class="punctuation token">.</span><span class="function token">add</span><span class="punctuation token">(</span><span class="string token">'foo'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>

mySet<span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span><span class="string token">'foo'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>  <span class="comment token">// 返回 true</span>
mySet<span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span><span class="string token">'bar'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>  <span class="comment token">// 返回 false</span>

<span class="keyword token">var</span> set1 <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Set</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="keyword token">var</span> obj1 <span class="operator token">=</span> <span class="punctuation token">{</span><span class="string token">'key1'</span><span class="punctuation token">:</span> <span class="number token">1</span><span class="punctuation token">}</span><span class="punctuation token">;</span>
set1<span class="punctuation token">.</span><span class="function token">add</span><span class="punctuation token">(</span>obj1<span class="punctuation token">)</span><span class="punctuation token">;</span>

set1<span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span>obj1<span class="punctuation token">)</span><span class="punctuation token">;</span>        <span class="comment token">// 返回 true</span>
set1<span class="punctuation token">.</span><span class="function token">has</span><span class="punctuation token">(</span><span class="punctuation token">{</span><span class="string token">'key1'</span><span class="punctuation token">:</span> <span class="number token">1</span><span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 会返回 false，因为其是另一个对象的引用</span>
set1<span class="punctuation token">.</span><span class="function token">add</span><span class="punctuation token">(</span><span class="punctuation token">{</span><span class="string token">'key1'</span><span class="punctuation token">:</span> <span class="number token">1</span><span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 现在 set1 中有2条（不同引用的）对象了</span></code></code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-set.prototype.has" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Set.prototype.has</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div><p></p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>38</td>
<td><a href="/en-US/Firefox/Releases/13" title="Released on 2012-06-05.">13.0</a> (13.0)</td>
<td>11</td>
<td>25</td>
<td>7.1</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td>38</td>
<td>13.0 (13.0)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>iOS 8</td>
</tr>
</tbody>
</table>
</div>
<h2 id="相关连接"><span style="font-size: 30px; letter-spacing: -1px; line-height: 30px;"><strong>相关连接</strong></span></h2>
<ul>
<li><a href="Reference/Global_Objects/Set" title="Set 对象允许你存储任何类型的唯一值，无论是原始值或者是对象引用。"><code>Set</code></a></li>
<li><a href="Reference/Global_Objects/Set/add" title="add() 方法用来向一个 Set 对象的末尾添加一个指定的值。"><code>Set.prototype.add()</code></a></li>
<li><a href="Reference/Global_Objects/Set/delete" title="delete() 方法可以从一个 Set 对象中删除指定的元素。"><code>Set.prototype.delete()</code></a></li>
</ul>
</article>